const getroles = async() => {
    try {
        let response = await ajax('getroles?pagenum=1&pagesize=30')
        let roles = response.data,
            role_select = document.querySelector('.role')
        for (const role of roles) {
            let option = document.createElement('option')
            option.innerHTML = role.rolename
            option.value = role.id
            role_select.add(option)
        }
    } catch (error) {
        alert(error.meta.msg)
    }
}

const roleskin_add = async() => {
    let skinname = document.querySelector('.skinname').value,
        cover = document.querySelector('.cover').value,
        price = parseInt(document.querySelector('.price').value),
        islimit_radios = document.querySelectorAll('.islimit'),
        islimit,
        role_select = document.querySelector('.role'),
        role = role_select.options[role_select.selectedIndex].value
    for (const raido of islimit_radios) {
        if (raido.checked) {
            islimit = raido.value
        }
    }
    if (!skinname || skinname.trim().length == 0) return alert('皮肤名称必填');
    if (!role || role.trim().length == 0) return alert('所属角色必选');
    if (!islimit || islimit.trim().length == 0) return alert('是否限量必选')

    try {
        let obj = {
            skinname,
            cover,
            price,
            islimit,
            roleid: role
        }
        const response = await ajax('addrole_skin', 'POST', obj)
        alert('添加皮肤成功')
        location.href = './role_skins.html'
    } catch (error) {
        alert(error.meta.msg)
    }
}

window.onload = function() {
    getroles()
}